package com.fingard.dsp.bank.directbank.vbao03.trade.utils;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder;


public class XMLConverter {
    /**
     * 由XML生成java对象
     *
     * @param <T>
     * @param xml
     * @param c
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T fromXML(String xml, Class<T> c) {
        T ret = null;
        XStream xs = new XStream(new DomDriver("UTF-8", new XmlFriendlyNameCoder("-_", "_")));
        xs.ignoreUnknownElements();
        xs.processAnnotations(c);
        ret = (T) xs.fromXML(xml);
        return ret;
    }

    /**
     * 由java对象生成XML
     *
     * @param <T>
     * @param t
     * @return
     */
    public static <T> String toXML(T t) {
        String xmlStr = null;
        XStream xs = new XStream(new DomDriver("UTF-8", new XmlFriendlyNameCoder("-_", "_")));
        xs.processAnnotations(t.getClass());
        xmlStr = xs.toXML(t);
        xmlStr = xmlStr.replaceAll("\t", "");
        return xmlStr;
    }
}
